package com.example.admin.service.Impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.admin.mapper.CategoryMapper;
import com.example.admin.service.CategoryService;

import com.example.xo.pojo.Category;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 992c
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    //注入mapper
    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public boolean add(Category category) {
        //调用名字重复的方法
        medo();
        //雪花id
        String snowflakeNextIdStr = IdUtil.getSnowflakeNextIdStr();
        category.setCategoryId(snowflakeNextIdStr);
        //如果res大于0就代表有变动，零就是没改
        int res = categoryMapper.insert(category);
        return res == 1;
    }

    @Override
    public List<Category> findAll() {
        //获取一个容器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //按创建时间降序排序                 获取创建时间
        wrapper.orderByDesc(Category::getCreated);
        //判断删除的时间是否为null，如果是就显示出来，反之就不要
        wrapper.isNull(Category::getDeleted);

        return categoryMapper.selectList(wrapper);
    }

    @Override
    public Category findById(String id) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getCategoryId, id);
        //判断删除的时间是否为null，如果是就显示出来，反之就不要
        wrapper.isNull(Category::getDeleted);
        return categoryMapper.selectOne(wrapper);
    }

    @Override
    public Category findByName(String name) {
        //获取一个容器用来接收条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //条件拼接
        wrapper.eq(Category::getCategoryName, name);


        return categoryMapper.selectOne(wrapper);
    }

    @Override
    public boolean deleteById(String id) {
        //todo 这里需要判断一次下面是否有博客，如果有就不可以删除
        Category category = findById(id);
        //获取现在时间
        category.setDeleted(LocalDateTime.now());
        //获取得数字是改变了几行
        int i = categoryMapper.updateById(category);
        //不等于零代表有变，等于零就代表没变
        return i > 0;
    }

    @Override
    public boolean update(Category category) {
        //调用名字重复的方法
        medo();
      LambdaQueryWrapper<Category>wrapper=new LambdaQueryWrapper<>();

      wrapper.eq(Category::getCategoryId,category.getCategoryId());
        int i = categoryMapper.update(category,wrapper);
        return i>0;
    }

    /**
     * 名字重复的方法
     * @return 名字重复返回false
     */
    private boolean medo() {
        Category category = new Category();
        Category byName = findByName(category.getCategoryName());
        //如果返回的值不是null就代表有重复，返回false
        if (byName != null) {
            log.info("分类{}", category.getCategoryName());
            return false;
        }
        return true;
    }
}
